package com.neo.office.server.controller.system;

import com.neo.office.server.config.data.Result;
import com.neo.office.server.domain.entity.User;
import com.neo.office.server.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 个人中心
 */
@RestController
public class UserInfoController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/user/info")
    public Result updateUserUser(@RequestBody User user, Authentication authentication){
        if (userService.updateById(user)){
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(
                    user,
                    null,
                    authentication.getAuthorities()));
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @ApiOperation(value = "更新用户密码")
    @PutMapping("/user/pass")
    public Result updateUserPassword(@RequestBody Map<String,Object> info){
        String oldPass = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
        Integer userId = (Integer) info.get("userId");
        return userService.updateUserPassword(oldPass,pass,userId);
    }

}
